<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>中心领导人员信息管理</title>
</head>
<link rel="stylesheet" href="../res/layui/css/layui.css" media="all">
<script src="../res/js/jquery-3.1.1.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../res/layui/layui.js"></script>


<body>
<script type="text/html" id="toolbarDemo">
        <div style="width: 100%;height: 30%;">
            <div style="position: absolute;left: 5%;top: 5%;">
                <button class="layui-btn" lay-event="add">新增领导</button>
            </div>
            <div style="position: absolute;left: 20%;top: 5%;">
                <button class="layui-btn layui-btn-danger" lay-event="deletes">批量删除</button>
            </div>
            <div style="width: 100%;position: absolute;left: 35%;top: 5%;">
                <input type="text" id="leaderName" placeholder="试试领导姓名？"style="width: 25%;" class="layui-input">
            </div>
            <div style="position: absolute;left: 60%;top: 5%;">
                <button class="layui-btn" lay-event="selectName" >搜索姓名</button>
            </div>

        </div>
</script>

    <div id = "updatModule">

    </div>
    <table id = "tab" lay-filter="test"></table>
    <script type="text/javascript">
            /*$.ajax({
                url:"../AtsEmpleader/findAll",
                type:"post",
                dataType:"json",
                // contentType: "application/json;charset=  utf-8",
                success:function(data){

                    $("#tab").
                },error:function(){
                    alert("没有返回");
                }
            });*/
            var tableTwo = null;
            var table = null;
            layui.use('table', function(){
                table = layui.table;
                tableTwo=table.render({
                    elem: '#tab'
                    ,height: 570
                    ,toolbar: '#toolbarDemo'
                    ,url: '../AtsEmpleader/findAll' //数据接口
                    ,page: true //开启分页
                    ,cols: [[ //表头
                        {type: 'checkbox', fixed: 'left'}
                        ,{field: 'leaderName', title: '领导姓名', fixed: 'left',width:120}
                        ,{field: 'deptName', title: '部门名称',width:120}
                        ,{field: 'postName', title: '岗位名称',width:120}
                        ,{field: 'leaderBusiness', title: '负责事物',width:120}
                        ,{field: 'leaderExp', title: '职责说明',width:120}
                        ,{field: 'phone', title: '联系电话',width:120}
                        ,{field: 'addr', title: '通讯地址',width:120}
                        ,{field: 'other', title: '其他说明',width:120}
                        //,{field: 'sort', title: '种类'}
                        //,{title: 'isva',title: '是否有效', toolbar: '#switch'}
                       // ,{field: 'creater', title: '创建人'}
                        //,{field: 'creatTime', title: '创建时间'}
                        //,{field: 'remark', title: '备注说明'}
                        ,{title: '操作',fixed: 'right', width:150, align:'center', toolbar: '#barDemo'}
                    ]]
                });
               //监听工具条
                table.on('tool(test)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                    var data = obj.data; //获得当前行数据
                    var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                    var tr = obj.tr; //获得当前行 tr 的DOM对象
                    console.info(data.leaderId);
                    if(layEvent === 'edit'){ //编辑
                        layer.open({
                            type: 2,
                            title:'用户修改页面',
                            shadeClose:true,
                            shade:0.8,
                            area:['80%','80%'],
                            content: ['AtsEmpLeaderUpdate.html?id='+data.leaderId], //数组第二项即吸附元素选择器或者DOM
                            end:function(){
                                //刷新表格
                                tableTwo.reload();
                            }
                        });
                    }else if(layEvent == 'del'){
                        layer.confirm('确定要删除本条记录吗？', {
                            btn: ['确定','取消'] //按钮
                        }, function(){
                            $.ajax({
                                url:"../AtsEmpleader/deleteLeader",
                                type:"post",
                                data:{
                                    id:data.leaderId
                                },
                                dataType:"json",
                                // contentType: "application/json;charset=  utf-8",
                                success:function(obj){
                                    layer.msg(obj.msg,{icon:1,time:1000},function(){
                                        //刷新表格
                                        tableTwo.reload();
                                    });

                                },error:function(){
                                    alert("服务器异常");
                                }
                            });
                        });
                    }
                });

                //头工具栏事件
                table.on('toolbar(test)', function(obj){
                    var data = obj.data;
                    var checkStatus = table.checkStatus(obj.config.id)
                        ,data = checkStatus.data; //获取选中的数据
                    //alert(obj.event);
                    switch(obj.event){
                        //增加
                        case 'add':
                            layer.open({
                                type: 2,
                                title:'用户新增页面',
                                shadeClose:true,
                                shade:0.8,
                                area:['80%','80%'],
                                content: ['AtsEmpLeaderUpdate.html'], //数组第二项即吸附元素选择器或者DOM
                                end:function(){
                                    //刷新表格
                                    tableTwo.reload();
                                }
                            });
                            break;
                        case 'deletes' :
                            //layer.msg("触发了批量删除");
                            //alert(data.length)
                            if(data.length == 0){
                                layer.msg('至少选择一行数据删除哦~');
                            }else {

                                //alert(JSON.stringify(data[0].athleteId));
                                var ids = [];//存放相应的用户Id
                                for(var i=0; i<data.length;i++){
                                    //alert(checkStatus.data[i].athleteId);
                                    ids.push(checkStatus.data[i].leaderId);
                                    //alert(ids[i]);
                                }
                                //发送ajax请求
                                $.ajax({
                                    url:"../AtsEmpleader/deleteCoachDates",
                                    type:"post",
                                    traditional:true,
                                    data:{
                                        ids:ids
                                    },
                                    dataType:"json",
                                    // contentType: "application/json;charset=utf-8",
                                    success:function(data){
                                        layer.msg(data.msg);
                                        tableTwo.reload();
                                    }
                                });

                            }
                            break;
                        case 'selectName':
                            //layer.msg("触发了搜索按钮");
                            var leaderName = $("#leaderName").val();
                            //表格重载
                            tableTwo.reload({
                                url:'../AtsEmpleader/findAll',
                                page: {
                                    curr: 1 //重新从第 1 页开始
                                }
                                ,where: {
                                    leaderName:$("#leaderName").val()
                                }
                            });
                            $("#leaderName").val(leaderName);
                    };
                });
            });


    </script>

    <script type="text/html" id="eq">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="addBtn">新增</button>
            <button class="layui-btn layui-btn-sm" lay-event="getCheckData">批量删除</button>
        </div>
    </script>

    <script type="text/html" id="switch">
        {{# if(d.isva=="1"){
            d.isva="checked" }}
            {{# } else{
                d.isva=" " }}
            {{# } }}
        <input type="checkbox" {{d.isva}} data-id ="{{d.leaderId}}" lay-filter="switchTest" lay-skin="switch" lay-text="否|是" {{ d.isva =="1" ?"checked" : "" }}/>

    </script>

    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>

        <!-- 这里同样支持 laytpl 语法，如： -->
        {{#  if(d.auth > 2){ }}
        <a class="layui-btn layui-btn-xs" lay-event="check">审核</a>
        {{#  } }}
    </script>
</body>
</html>

